package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.dragon.repository.entity.Comment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 评论表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-07-24
 */
@Mapper
public interface CommentMapper extends BaseMapper<Comment> {


    /**
     * 父级评论list
     * @param wrapper
     * @return
     */
    @Select("SELECT M.* FROM (select dc.*, da.username as username, da.img as avatar  " +
            "from dragon_comment dc left join dragon_admin da on dc.admin_id = da.id )M " +
            "${ew.customSqlSegment} ")
    List<Comment> findByParentIdNull(@Param(Constants.WRAPPER) QueryWrapper wrapper);

    /**
     * 查询一级回复
     * @param id
     * @return
     */
    @Select("SELECT M.*, da1.username as replyUsername FROM (select dc.*, da.username as username , da.img as avatar " +
            "from dragon_comment dc left join dragon_admin da on dc.admin_id = da.id )M " +
            "LEFT JOIN dragon_admin da1 on M.reply_admin_id = da1.id " +
            "where M.parent_id = #{id} " +
            "order by M.create_time desc ")
    List<Comment> findByParentIdNotNull(Integer id);

    /**
     * 查询二级以及所有子集回复
     * @param childId
     * @return
     */
    @Select("SELECT M.*, da1.username as replyUsername FROM (select dc.*, da.username as username,  da.img as avatar " +
            "from dragon_comment dc left join dragon_admin da on dc.admin_id = da.id )M " +
            "LEFT JOIN dragon_admin da1 on M.reply_admin_id = da1.id " +
            "where M.parent_id = #{childId} " +
            "order by M.create_time desc")
    List<Comment> findByReplayId(Integer childId);
}
